﻿using System.Collections.ObjectModel;
using Startup.Base.Commands;
using Startup.MVVM;
using Startup.Shell.Domain;
using Startup.Shell.Modules.ClassEditorModule;

namespace Startup.Shell.Modules.ClassesModule
{
	public class ClassesViewModel : ViewModelBase
	{
		private readonly ClassesModel _model;
		private StartupClass _selectedClassItem;

		public ClassesViewModel(ClassesModel model)
		{
			_model = model;
		}

		public ObservableCollection<StartupClass> Classes
		{
			get { return _model.Classes; }
		}

		public static ClassesViewModel Designer
		{
			get
			{
				var classes = new ObservableCollection<StartupClass>(new[]
				{
					new StartupClass { Name = "Class1" },
					new StartupClass { Name = "Class2" },
					new StartupClass { Name = "Class3" },
					new StartupClass { Name = "Class4" }
				});

				var model = new ClassesModel(classes);
				return new ClassesViewModel(model);
			}
		}

		public ClassEditorViewModel SelectedViewModel
		{
			get
			{
				if (_selectedClassItem == null)
					return null;

				var model = new ClassEditorModel(_selectedClassItem);
				return new ClassEditorViewModel(model);
			}
		}

		public StartupClass SelectedClassItem
		{
			get { return _selectedClassItem; }
			set
			{
				if (_selectedClassItem == value)
					return;

				_selectedClassItem = value;

				RaisePropertyChanged("SelectedClassItem");
				RaisePropertyChanged("SelectedViewModel");
			}
		}

		public SimpleCommand NewClassCommand
		{
			get { return new SimpleCommand(() => _model.Classes.Insert(0, new StartupClass { Name = "NewClass" }), () => _model != null); }
		}
	}
}
